package com.globalwave.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.globalwave.common.db.SqlParser;
import com.globalwave.test.BaseTestCase;
import static org.junit.Assert.*;

public class SqlParserTest extends BaseTestCase {

	@Test
	public void paser() throws Exception {
		String sql = "hello [p1]!" ;
		Map<String, Object> mbean = new HashMap<String, Object>() ;
		mbean.put("p1", "world") ;
		SqlParser parser = new SqlParser(sql, mbean) ;

		assertEquals("hello ?!", parser.parse()) ;
		assertEquals("world", parser.getParams().get(0)) ;
		
	}

	@Test
	public void paser2() throws Exception {
		String sql = "hello [p1]!{good moring [p2]!}" ;
		Map<String, Object> mbean = new HashMap<String, Object>() ;
		mbean.put("p1", "world") ;
		SqlParser parser = new SqlParser(sql, mbean) ;

		assertEquals("hello ?!", parser.parse()) ;
		assertEquals("world", parser.getParams().get(0)) ;

		mbean.put("p2", "cxr") ;
		parser = new SqlParser(sql, mbean) ;

		assertEquals("hello ?!good moring ?!", parser.parse()) ;
		assertEquals("world", parser.getParams().get(0)) ;
		assertEquals("cxr", parser.getParams().get(1)) ;
		
	}

	@Test
	public void paser3() throws Exception {
		String sql = "hello ([p1])!" ;
		Map<String, Object> mbean = new HashMap<String, Object>() ;
		List<String> guys = new ArrayList<String>() ;

		guys.add("you") ;
		guys.add("me") ;
		
		mbean.put("p1", guys) ;
		SqlParser parser = new SqlParser(sql, mbean) ;

		assertEquals("hello (?, ?)!", parser.parse()) ;
		assertEquals("you", parser.getParams().get(0)) ;
		assertEquals("me", parser.getParams().get(1)) ;
		
		PBean pBean = new PBean() ;
		pBean.setP1(guys) ;

		parser = new SqlParser(sql, pBean) ;

		assertEquals("hello (?, ?)!", parser.parse()) ;
		assertEquals("you", parser.getParams().get(0)) ;
		assertEquals("me", parser.getParams().get(1)) ;
	}
	
	public class PBean {
		private List<String> p1 ;

		public List<String> getP1() {
			return p1;
		}

		public void setP1(List<String> p1) {
			this.p1 = p1;
		}
		
	}
}
